package com.hildeberto.versions.model;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public enum Config {

    PROPERTIES;

    private java.util.Properties properties = new java.util.Properties();

    private Config() {
        try {
            properties.load(new FileInputStream("config.properties"));
        }
        catch(IOException ioe) {
            saveProperties(true);
        }
    }

    public List<String> getHistoryMessageList() {
        List<String> historyMessages = new ArrayList<String>();
        for(int i = 0;i < 10;i++) {
            historyMessages.add(properties.getProperty("historyMessage"+ i));
        }
        return historyMessages;
    }

    public void addMessageToHistory(String message) {
        for (int i = 8; i >= 0; i--) {
            properties.setProperty("historyMessage"+ (i + 1), properties.getProperty("historyMessage" + i));
        }
        properties.setProperty("historyMessage0", message);
        saveProperties(false);
    }

    private void saveProperties(boolean create) {
        try {
            if(create) {
                for(int i = 0;i < 10;i++) {
                    properties.setProperty("historyMessage"+ i, "");
                }
            }
            properties.store(new FileOutputStream("config.properties"), null);
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
